<!-- 困难证明 -->
<template>
  <view class="view-content">
    <s-nav title="申请人信息" />
    <s-content>
      <view class="main">
        <!-- 列表 -->
        <view class="applicant-from">
          <uni-forms :modelValue="formData" ref="form" :rules="rules">
            <uni-forms-item required label="姓名" name="name">
              <uni-easyinput
                v-model="formData.name"
                type="text"
                placeholder="请输入"
                placeholder-class="placeholder-style"
              />
            </uni-forms-item>
            <uni-forms-item required name="gender" label="性别">
              <s-picker
                title="选择性别"
                :dataSet="array"
                @change="change($event, 'gender')"
              />
            </uni-forms-item>
            <uni-forms-item required name="nation" label="民族">
              <s-picker
                title="选择民族"
                :dataSet="array2"
                @change="change($event, 'nation')"
              />
            </uni-forms-item>
            <uni-forms-item required name="address" label="家庭地址">
              <uni-easyinput
                v-model="formData.address"
                type="text"
                placeholder="请输入"
              />
            </uni-forms-item>
            <uni-forms-item required name="cardNum" label="身份证号">
              <uni-easyinput
                v-model="formData.cardNum"
                type="idcard"
                maxlength="18"
                placeholder="请输入"
              />
            </uni-forms-item>
            <uni-forms-item required name="familySize" label="家庭人数">
              <s-picker
                title="选择人数"
                :dataSet="array3"
                @change="change($event, 'familySize')"
              />
            </uni-forms-item>
            <uni-forms-item required name="familyIncome" label="家庭年收入">
              <uni-easyinput
                v-model="formData.familyIncome"
                type="digit"
                placeholder="请输入"
              />
            </uni-forms-item>
            <uni-forms-item required name="difficultyCause" label="困难原因">
              <uni-easyinput
                v-model="formData.difficultyCause"
                type="text"
                placeholder="请输入"
              />
            </uni-forms-item>
            <uni-forms-item
              required
              name="majorIncomeSource"
              label="主要收入来源"
            >
              <uni-easyinput
                v-model="formData.majorIncomeSource"
                type="text"
                placeholder="请输入"
              />
            </uni-forms-item>
          </uni-forms>
        </view>
        <view class="footer-container">
          <s-btn
            title="提交"
            @submit="submit"
            style="width: 100%; background: #02b494"
          />
        </view>
      </view>
    </s-content>
  </view>
</template>

<script>
import { nation } from "../static/js/nationality.js";
import { checkIsJSON } from "../static/js/typeJson.js";
import { post } from "../../utils/request";
export default {
  data() {
    return {
      formData: {}, // 表单数据
      array: ["男", "女"],
      array2: [
        // 民族
      ],
      array3: [1, 2, 3, 4, 5], // 家庭人数值域
      rules: {
        // 表单校验规则
        name: {
          rules: [
            {
              required: true,
              errorMessage: "请输入姓名",
            },
          ],
        },
        gender: {
          rules: [
            {
              required: true,
              errorMessage: "请选择性别",
            },
          ],
        },
        nation: {
          rules: [
            {
              required: true,
              errorMessage: "请选择民族",
            },
          ],
        },
        address: {
          rules: [
            {
              required: true,
              errorMessage: "请输入家庭住址",
            },
          ],
        },
        cardNum: {
          rules: [
            {
              required: true,
              errorMessage: "请输入身份证号",
            },
          ],
        },
        familyIncome: {
          rules: [
            {
              required: true,
              errorMessage: "请输入家庭年收入",
            },
          ],
        },
        difficultyCause: {
          rules: [
            {
              required: true,
              errorMessage: "请输入困难原因",
            },
          ],
        },
        majorIncomeSource: {
          rules: [
            {
              required: true,
              errorMessage: "请输入主要收入来源",
            },
          ],
        },
      },
    };
  },
  /**
   * 接收页面传参
   * @param {object} option
   */
  onLoad(option) {
    if (option.params && checkIsJSON(option.params)) {
      this.formData = {
        ...this.formData,
        ...JSON.parse(option.params),
      };
    }
  },
  /**
   * 初始化
   */
  created() {
    this.array2 = nation;
  },
  onReady() {
    // 需要在onReady中设置规则
    this.$refs.form.setRules(this.rules);
  },
  methods: {
    /**
     * TextPicker回调表单赋值
     * @param {Object} value 回调数据
     * @param {Object} code	表单赋值的name
     */
    change(value, code) {
      this.formData[code] = value;
      this.$refs.form.setValue(code, value);
    },
    /*
     * 表单提交
     */
    submit() {
      this.$refs.form
        .validate()
        .then((res) => {
          uni.showLoading({
            title: "提交中",
            mask: true,
          });
          post("/api/application/xsbsCertifyInfo/app/saveCertifyInfo", {
            ...res,
            ...this.formData,
          })
            .then((value) => {
              if (value.code === "000000") {
                uni.showToast({
                  title: "操作成功",
                  duration: 1000,
                });
                return uni.navigateBack({
                  delta: 2,
                  success: () => {
                    uni.showToast({
                      title: value.message || "操作失败",
                      duration: 1000,
                    });
                  },
                });
              }
            })
            .catch(() => {
              uni.hideLoading();
            });
        })
        .catch((err) => {
          console.log("表单错误信息：", err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../static/css/form.scss";
</style>
